package com.hzit.mmcb.core.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzit.mmcb.core.pojo.dto.ExcelDict;
import com.hzit.mmcb.core.pojo.entity.Dict;
import com.hzit.mmcb.core.mapper.DictMapper;
import com.hzit.mmcb.core.service.DictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author ljh
 * @since 2024-08-27
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {
    /**
     * 上传文件
     * @param inputStream
     */
    @Override
    public void upload(InputStream inputStream) {
        EasyExcel.read(inputStream, ExcelDict.class,new MyReadListener(this)).sheet().doRead();
    }

    /**
     * 下载文件
     * @return  返回DTO列表
     */
    @Override
    public List<ExcelDict> listExcelDictDTO() {
        List<Dict> list = this.list();
        List<ExcelDict> excelDicts=new ArrayList<>();
        list.stream().map(dict -> {
            ExcelDict excelDict = new ExcelDict();
            BeanUtils.copyProperties(dict,excelDict);
            excelDicts.add(excelDict);
            return dict;
        }).collect(Collectors.toList());

        return excelDicts;
    }

    @Override
    public List<Dict> findById(Integer pid) {
        QueryWrapper<Dict> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",pid);
        List<Dict> list = this.list(queryWrapper);
        List<Dict> parentId = list.stream().map(m -> {
            if (this.count(new QueryWrapper<Dict>().eq("parent_id", m.getId())) > 0) {
                // m.setChildren(true);
                m.setHasChildren(true);
            }
            ;
            return m;
        }).collect(Collectors.toList());
        return parentId;
    }

    @Override
    public List<Dict> findByCode(String code) {
        Dict dict = this.getOne(new LambdaQueryWrapper<Dict>().eq(Dict::getDictCode, code));
        List<Dict> dictList = findById(dict.getId().intValue());
        return dictList;
    }


    private class MyReadListener extends AnalysisEventListener<ExcelDict> {
        private DictService service;
        private final int SIZE=10;
        List<Dict> list=new ArrayList<>();

        public MyReadListener(DictServiceImpl dictService) {
            this.service=dictService;
        }

        @Override
        public void invoke(ExcelDict excelDict, AnalysisContext analysisContext) {

            Dict dict = new Dict();
            BeanUtils.copyProperties(excelDict,dict);
            list.add(dict);
            if (list.size()>=SIZE){
                savemysql(list);
                list.clear();
            }
        }



        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
            service.saveBatch(list);
        }
        private void savemysql(List<Dict> list) {
            service.saveBatch(list);
        }
    }
}
